package com.huizu.project.apiController;

import cn.hutool.core.collection.CollUtil;
import com.huizu.common.annotation.Anonymous;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.core.page.TableDataInfo;
import com.huizu.common.enums.BusinessType;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.project.domain.ProXy;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.IProXyService;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 学院管理Controller
 *
 * @author zhangmx
 * @date 2024-05-11
 */
@Anonymous
@RestController
@RequestMapping("/api/project/proxy")
public class ApiProXyController extends BaseController
{
    @Autowired
    private IProXyService proXyService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询学院管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:proxy:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProXy proXy) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> xyIds = auths.stream().filter(auth -> auth.getType().equals("xy")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proXy.setXyIds(xyIds);
        }
        startPage();
        List<ProXy> list = proXyService.selectProXyList(proXy);
        return getDataTable(list);
    }

    /**
     * 查询学院管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:proxy:list')")
    @PostMapping("/listTotal")
    public TableDataInfo listTotal(HttpServletRequest request, ProXy proXy) {
        String token = request.getHeader(TokenUtils.getAppTokenHeader());
        String userId = TokenUtils.parseToken(token);
        if (!SecurityUtils.isAdmin(Long.parseLong(userId))) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(Long.parseLong(userId));
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> xyIds = auths.stream().filter(auth -> auth.getType().equals("xy")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proXy.setXyIds(xyIds);
        }
        List<ProXy> list = proXyService.selectProXyList(proXy);
        return getDataTable(list);
    }

    /**
     * 导出学院管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:proxy:export')")
    @Log(title = "学院管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProXy proXy)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> xyIds = auths.stream().filter(auth -> auth.getType().equals("xy")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proXy.setXyIds(xyIds);
        }
        List<ProXy> list = proXyService.selectProXyList(proXy);
        ExcelUtil<ProXy> util = new ExcelUtil<ProXy>(ProXy.class);
        util.exportExcel(response, list, "学院管理数据");
    }

    /**
     * 获取学院管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:proxy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(proXyService.selectProXyById(id));
    }

    /**
     * 新增学院管理
     */
    @PreAuthorize("@ss.hasPermi('project:proxy:add')")
    @Log(title = "学院管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProXy proXy)
    {
        ProXy param = new ProXy();
        param.setNumber(proXy.getNumber());
        List<ProXy> list = proXyService.selectProXyList(param);
        if (CollUtil.isNotEmpty(list)){
            throw new ServiceException("对不起，您输入的学院编号重复[" +proXy.getNumber() + "]");
        }
        return toAjax(proXyService.insertProXy(proXy));
    }

    /**
     * 修改学院管理
     */
    @PreAuthorize("@ss.hasPermi('project:proxy:edit')")
    @Log(title = "学院管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProXy proXy) {
        ProXy param = new ProXy();
        param.setNumber(proXy.getNumber());
        param.setId(proXy.getId());
        List<ProXy> list = proXyService.selectProXyListByNumberNoId(param);
        if (CollUtil.isNotEmpty(list)){
            throw new ServiceException("对不起，您输入的学院编号重复[" +proXy.getNumber() + "]");
        }
        return toAjax(proXyService.updateProXy(proXy));
    }

    /**
     * 删除学院管理
     */
    @PreAuthorize("@ss.hasPermi('project:proxy:remove')")
    @Log(title = "学院管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proXyService.deleteProXyByIds(ids));
    }
}
